package org.ling.xuan.condition;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.vfs.VirtualFile;
import org.ling.xuan.dao.LocalAddDao;
import org.ling.xuan.dao.MarkFileDao;
import org.ling.xuan.dao.ProjectDao;
import org.ling.xuan.util.LxOptional;
import org.ling.xuan.util.LxStrUtil;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LxCondition {
    private static final Lock lock = new ReentrantLock();
    private static volatile LxCondition condition;

    private LxCondition() {
    }

    public static LxCondition getInstance() {
        if (condition == null) {
            lock.lock();
            try {
                if (condition == null) {
                    condition = new LxCondition();
                }
            } finally {
                lock.unlock();
            }
        }
        return condition;
    }

    public boolean projectAddCondition(AnActionEvent e) {
        String basePath = "";
        if (e.getProject() != null && e.getProject().getBasePath() != null) {
            basePath = e.getProject().getBasePath();
            DataContext dataContext = e.getDataContext();
            // 通过 DataContext 获取当前文件
            VirtualFile virtualFile = dataContext.getData(CommonDataKeys.VIRTUAL_FILE);
            // 检查当前文件是否为有效文件
            if (virtualFile != null && virtualFile.isValid()) {
                // 获取当前文件的路径
                String filePath = virtualFile.getPath();
                LxStrUtil.unionPath(filePath);
                LxStrUtil.unionPath(basePath);
                return !ProjectDao.added(basePath);
            }
        }
        return false;
    }

    public boolean addCondition(AnActionEvent e) {
        String basePath = LxOptional.of(e, c -> c.getProject().getBasePath()).orElse("");
        LxStrUtil.unionPath(basePath);
        if (ProjectDao.added(basePath)) {
            return LocalAddDao.canLocalAdd();
        }
        return false;
    }

    public boolean checkCondition(AnActionEvent e) {
        String filePath = LxOptional.of(e, c -> c.getDataContext().getData(CommonDataKeys.VIRTUAL_FILE).getPath()).orElse("");
        LxStrUtil.unionPath(filePath);
        if (StrUtil.isNotBlank(filePath) && (filePath.endsWith(".java") || filePath.endsWith(".sql") || filePath.endsWith(".upd"))) {
            return FileUtil.isFile(filePath);
        } else {
            return false;
        }
    }

    public boolean checkEditCurrCondition(AnActionEvent e) {
        String basePath = e.getProject().getBasePath();
        LxStrUtil.unionPath(basePath);
        return ProjectDao.added(basePath);
    }
    
    public boolean canAnnotationDeal(AnActionEvent e) {
        String filePath = LxOptional.of(e, c -> c.getDataContext().getData(CommonDataKeys.VIRTUAL_FILE).getPath()).orElse("");
        LxStrUtil.unionPath(filePath);
        return StrUtil.isNotBlank(filePath) && (filePath.endsWith(".java"));
    }

    public boolean markCondition(AnActionEvent e) {
        String basePath = LxOptional.of(e, c -> c.getProject().getBasePath()).orElse("");
        LxStrUtil.unionPath(basePath);
        if (ProjectDao.added(basePath)) {
            String filePath = LxOptional.of(e, c -> c.getDataContext().getData(CommonDataKeys.VIRTUAL_FILE).getPath()).orElse("");
            LxStrUtil.unionPath(filePath);
            if (FileUtil.isFile(filePath)) {
                if (StrUtil.isNotBlank(basePath) && StrUtil.isNotBlank(filePath)) {
                    return MarkFileDao.canMark(filePath, null);
                }
            }
        }
        return false;
    }

    public boolean unMarkCondition(AnActionEvent e) {
        String basePath = LxOptional.of(e, c -> c.getProject().getBasePath()).orElse("");
        LxStrUtil.unionPath(basePath);
        if (ProjectDao.added(basePath)) {
            String filePath = LxOptional.of(e, c -> c.getDataContext().getData(CommonDataKeys.VIRTUAL_FILE).getPath()).orElse("");
            LxStrUtil.unionPath(filePath);
            if (FileUtil.isFile(filePath)) {
                if (StrUtil.isNotBlank(basePath) && StrUtil.isNotBlank(filePath)) {
                    return !MarkFileDao.canMark(filePath, null);
                }
            }
        }
        return false;
    }

    public boolean projectRemoveCondition(AnActionEvent e) {
        String basePath = "";
        if (e.getProject() != null && e.getProject().getBasePath() != null) {
            basePath = e.getProject().getBasePath();
            LxStrUtil.unionPath(basePath);
            DataContext dataContext = e.getDataContext();
            // 通过 DataContext 获取当前文件
            VirtualFile virtualFile = dataContext.getData(CommonDataKeys.VIRTUAL_FILE);
            // 检查当前文件是否为有效文件
            if (virtualFile != null && virtualFile.isValid()) {
                // 获取当前文件的路径
                String filePath = virtualFile.getPath();
                LxStrUtil.unionPath(filePath);
                if (filePath.equals(basePath)) {
                    return ProjectDao.added(basePath);
                }
            }
        }
        return false;
    }

    public boolean copyCondition(AnActionEvent e) {
        String basePath = "";
        if (e.getProject() != null && e.getProject().getBasePath() != null) {
            basePath = e.getProject().getBasePath();
            LxStrUtil.unionPath(basePath);
            return ProjectDao.added(basePath);
        }
        return false;
    }
    
    public boolean isDirectory(AnActionEvent e){
        boolean flag = false;
        DataContext dataContext = e.getDataContext();
        VirtualFile virtualFile = dataContext.getData(CommonDataKeys.VIRTUAL_FILE);
        // 检查当前文件是否为有效文件
        if (virtualFile != null && virtualFile.isValid()) {
            // 获取当前文件的路径
            String filePath = virtualFile.getPath();
            flag = FileUtil.isDirectory(filePath);
        }
        return flag;
    }
}
